package com.hunlian.makelove.net;

import android.os.Handler;
import android.text.TextUtils;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.tools.LogTools;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;


public class CustomerOkHttpClient {

    public static OkHttpClient client;
    private static final int DEFAULT_TIMEOUT = 30*1000;
    public static Handler okHttpHandler;//全局处理子线程和M主线程通信

    private CustomerOkHttpClient() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    private static void create() {
        int maxCacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(AppApplication.getAppContext().getCacheDir(), maxCacheSize);
        Interceptor cacheInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Response response = chain.proceed(request);

                String cacheControl = request.cacheControl().toString();
                if (TextUtils.isEmpty(cacheControl)) {
                    cacheControl = "public, max-age=60 ,max-stale=2419200";
                }
                return response.newBuilder()
                        .header("Cache-Control", cacheControl)
                        .removeHeader("Pragma")
                        .build();
            }
        };

        // add your other interceptors …add logging as last interceptor
        client = new OkHttpClient.Builder()
                .addNetworkInterceptor(cacheInterceptor)
                .addInterceptor(new LoggingInterceptor())
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .cache(cache)
                .build();

        //初始化Handler
        okHttpHandler = new Handler(AppApplication.getAppContext().getMainLooper());
    }

    public static OkHttpClient getClient() {
        if (client == null) {
            create();
        }
        return client;
    }

    static class LoggingInterceptor implements Interceptor {
        private static final String TAG = "CustomerOkHttpClient";

        @Override
        public Response intercept(Chain chain)  throws IOException{

                    Request request = chain.request();
                    long t1 = System.nanoTime();
                    LogTools.e(String.format("Request: %s", request.url()));

                        Response response = chain.proceed(request);

                        long t2 = System.nanoTime();
                        String responseString = new String(response.body().bytes());
                        LogTools.e(String.format("Access Server in %.1fms ,Response: %s", (t2 - t1) / 1e6d,responseString));
                        return response.newBuilder().body(ResponseBody.create(response.body().contentType(), responseString)).build();

        }
    }


}
